package com.sundae.interceptor;


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by superace on 2017/8/9.
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor 拦截");
        if(request.getSession().getAttribute("user") != null)
            return true;

        response.setHeader("Content-Type" , "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("<script>alert('请登录');window.location.href='/Login'</script>");
        return false;
    }
}
